<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22 0022
 * Time: 上午 10:18
 */

namespace App\Http\Controllers\Xcx;


use App\Craftsman;
use App\Exceptions\CreateException;
use App\Extend\Tools;
use App\Http\Controllers\Controller;
use App\Item;
use App\Member;
use App\Project;
use App\Needwork;
use Carbon\Carbon;
use App\Xm_history;
use App\Gc_history;
use App\Xm_register;
use App\Area;
use App\Guest;
use Illuminate\Http\Request;

/**
 * @property Craftsman model
 */
class ProjectController extends Controller
{
    public function __construct(Craftsman $model)
    {
        $this->model = $model;
    }
    public function project() //将所有审核过的项目遍历出来
    {
        $guest = Guest::where('id',auth('xcx')->user()->id)->first();
        if($guest->members_id == '') {
            return $this->failed(307,'请完善信息');
        }
        $data = Xm_register::where('shenhe','=','4')->get();
        $list = [];
        return $this->success($data);
        foreach ($data as $value) {
            $tmp=[];
            $tmp['id'] = $value['id'];
            $tmp['iconPath']="/images/icon/project.png";
            $tmp['longitude']=floatval($value['longitude']);
            $tmp['latitude']=floatval($value['latitude']);
            $tmp['width']=24;
            $tmp['height']=30;
            $tmp['callout']=[];
            $tmp['callout']['content']="项目负责人姓名：".$value['x_name']."\n项目负责人电话:".$value['phone'];
            $tmp['callout']['color']='#FFFFFF';
            $tmp['callout']['borderRadius']=10;
            $tmp['callout']['bgColor']='#000000';
            $tmp['callout']['fontSize']=16;
            $tmp['callout']['padding']=10;
            array_push($list,$tmp);
        }
        return $this->success($list);
    }
    public function projectinfo(Request $request,$id) //需要当前点击的项目id
    {
        $data = Xm_register::find($id);
        $n = Gc_history::where('xm_id',$id)->first();
        if($n == null) {
            if(auth('xcx')->user()->type == 1) { //判断当前是班组在查看项目
                $gc_history = new Gc_history;
                $gc_history -> xm_id = $id; //将当前项目的id 存入历史表中
                $gc_history -> id = auth('xcx')->user()->id; //将当前是哪个班组查看的项目历史记录存入表中，以便于区分
                $gc_history -> save();
            }
        }
        return $this->success($data); //将当前项目的数据分配过去
    }
    
    public function liulanbz(Request $request) //浏览的项目记录
    {
        $page = $request->input('page',1);
        $size = $request->input('size',10);
        $res = Gc_history::where('id',auth('xcx')->user()->id)->limit($size)->offset(($page-1)*$size)->get();
        $ids = [];
        foreach ($res as $v) {
            $ids[] = $v->xm_id; // 将浏览过的项目id存入数组中
        }
        $data = Craftsman::with('header')->with('project')->whereIn('id',$ids)->get()->toArray();
        $list = [];
        foreach ($data as $value){
            $tmp = [];
            $tmp['name'] = $value['name'];
            $tmp['auth'] = 1;
            $tmp['number'] = $value['mens'];
            $tmp['city'] = $value['header']['hope_city'];
            $tmp['time']  = date('Y-m-d',strtotime($value['updated_at']));
            $tmp['images'] = $value['images'][0];
            $tmp['id'] = $value['id'];
            $tmp['nation'] = $value['header']['origin']??'汉';
            $tmp['team'] = $value['project']['name'];
            array_push($list,$tmp);
        }
        return $this->success(compact('list'));
    }
    public function xmgz()
    {
        $id = auth('xcx')->user()->id;
        $s = Xm_register::where('uid',$id)->get();//把当前项目负责人和电话以及项目名称传过去
        $arr = [];
        foreach ($s as $k=>$v) {
            $n = Area::where('id',$v->x_province)->first();
            $s = Area::where('id',$v->x_city)->first();
            $arr[$k]['x_name'] = $n->name.str_replace('市辖区','',$s['name']); 
            $arr[$k]['phone'] = $v->phone; // 项目负责人电话
            $arr[$k]['name'] = $v->name; // 项目名称
        }
        return $this->success($arr);
    }
    
}